package com.dayouzi.crawler_monitor.utils;
 
import com.github.pagehelper.util.StringUtil;

import java.util.HashMap;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * 驼峰与下划线互转
 */
public class HumpToUnderLineUtil {
 
 
    public static void main(String[] args) {

        String errCount = HumpToUnderLineUtil.humpToUnderline("errCount");
        System.out.println(errCount);
    }
 
 
    /**
     * 驼峰转为下划线
     *
     * @param str
     * @return
     */
    public static String humpToUnderline(String str) {
        StringBuffer sb = new StringBuffer();
        if (StringUtil.isNotEmpty(str)) {
            Matcher matcher = Pattern.compile("[A-Z]").matcher(str);
            while (matcher.find()) {
                matcher.appendReplacement(sb, "_" + matcher.group(0).toLowerCase());
            }
            matcher.appendTail(sb);
        }
        return sb.toString();
    }
 
 
    /**
     * 下划线转为驼峰
     *
     * @param str
     * @return
     */
    public static String underlineToHump(String str) {
        StringBuffer sb = new StringBuffer();
        if (StringUtil.isNotEmpty(str)) {
            str = str.toLowerCase();
            Matcher matcher = Pattern.compile("_[a-z]").matcher(str);
            while (matcher.find()) {
                matcher.appendReplacement(sb, matcher.group(0).toUpperCase().replace("_", ""));
            }
            matcher.appendTail(sb);
        }
        return sb.toString();
    }
 
}